<?php

namespace App\Console\Commands;

use App\Models\Archive;
use Illuminate\Console\Command;

class RemoveArchive extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'zx:remove-archive {f=1}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    public $removeTotal = 0;
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $count = $this->argument('f');
        Archive::withCount('files')->cursor()->each(function ($archive) use ($count) {
            if ($archive->files_count <= $count) {
                $this->info(sprintf('删除文档：%d -> %s', $archive->id, $archive->title));
                $archive->delete();
                $this->removeTotal++;
            }
        });
        $this->info(sprintf('完成操作,共删除文档 %d 个', $this->removeTotal));
    }
}
